package com.sita.bike.ui.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sita.bike.R;
import com.sita.bike.ui.activity.DataAnalysisActivity;

import java.util.LinkedList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class YearGatherFragment extends Fragment {
    @Bind(R.id.recyclerHorizontalView)
    public RecyclerView recyclerView;
    public static YearGatherFragment instance;
    private int recyclerViewWidth;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    public static YearGatherFragment getInstance(){
        instance=new YearGatherFragment();
        return instance;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_year_gather, container, false);
        ButterKnife.bind(this, view);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(layoutManager);
       final List dataset = new LinkedList();
        for(int t=2013;t<2020;t++){
            if(t<2016||t>2016){
                dataset.add("");
            }else {
                dataset.add(""+t);
            }
        }

        //获取recyclerView 的宽度
        ViewTreeObserver vto = recyclerView.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                recyclerView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                recyclerViewWidth = recyclerView.getWidth();
                MyAdapter adapter = new MyAdapter(dataset, recyclerViewWidth);
                recyclerView.setAdapter(adapter);
                //获得现在的月份，然后移动到该月份
                int month = DataAnalysisActivity.getSystemMonth();//从零开始
                recyclerView.scrollToPosition(month);

            }
        });


//        MyAdapter adapter=new MyAdapter(dataset);
//        recyclerView.setAdapter(adapter);
        return view;
    }

    class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{

        public List<String> mDataset;
        public int recyclerViewWidth;

        public MyAdapter(List<String> dataset,int width) {
            super();
            mDataset = dataset;
            recyclerViewWidth = width;
        }

        @Override
        public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = View.inflate(viewGroup.getContext(), R.layout.month_list_item, null);
            ViewHolder holder = new ViewHolder(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) {
            LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) holder.mTextView.getLayoutParams();
            // 取控件aaa当前的布局参数
            linearParams.width = recyclerViewWidth / 7; // 当控件的高强制设成365象素
            holder.mTextView.setLayoutParams(linearParams); // 使设置好的布局参数应用到控件aaa
            holder.mTextView.setText(mDataset.get(position).toString());
            int year=DataAnalysisActivity.getSystemYear();
            if(String.valueOf(DataAnalysisActivity.getSystemYear()).equals(mDataset.get(position))){
                DataAnalysisActivity.getInstance().setCurrentYear(Integer.valueOf(holder.mTextView.getText().toString()));
            }
            if (position < 3 || position > 3) {
                holder.mImageSign.setBackgroundColor(getResources().getColor(R.color.data_analysis_background));
            } else {
                holder.mImageSign.setBackground(getResources().getDrawable(R.drawable.btn_bg_selected));
            }

            final ViewHolder viewHolder=holder;
            holder.mMonthListItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int toPosition = Integer.parseInt(viewHolder.mTextView.getText().toString());
                    RecyclerView.LayoutManager mLayoutManager = recyclerView.getLayoutManager();
                    ((LinearLayoutManager) mLayoutManager).scrollToPositionWithOffset(toPosition, 0);
                    DataAnalysisActivity.getInstance().setCurrentYear(Integer.valueOf(viewHolder.mTextView.getText().toString()));
                }
            });
        }

        @Override
        public int getItemCount() {
            return mDataset.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder{
            public LinearLayout mMonthListItem;
            public TextView mTextView;
            public ImageView mImageSign;
            public ViewHolder(View itemView) {
                super(itemView);
                mMonthListItem = (LinearLayout) itemView.findViewById(R.id.month_list_item);
                mTextView = (TextView)itemView.findViewById(R.id.tv_date);
                mImageSign=(ImageView)itemView.findViewById(R.id.sign_date);

            }
        }
    }
}
